<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

class VA_Pagination_Arrow {

    protected $article = null;
    /**
     *
     * @var stdClass
     */
    protected $prev = null;
    /**
     *
     * @var stdClass
     */
    protected $next = null;
    public function  __construct($items, stdClass $current) {

        $this->article = $current;
        $date = strtotime($current->created);
        $prev = $date;
        $id_prev = null;
        $next = $date;
        $id_next = null;

        foreach ($items as $el) {
            $cand = strtotime($el->created);
            if( $cand > $date) {
                if(($cand < $next) || (null === $this->next)) {
                    $next = $cand;
                    $this->next = $el;
                }
            } elseif($cand < $date)
                if(($cand > $prev) || (null === $this->prev)) {
                    $prev = $cand;
                    $this->prev = $el;
                }
        }



    }

    public function Next() {
        $attr = new VA_AttributeDictionary();
        $attr->Set('class', 'navigation');
        if(null !== $this->next) {
            $from = date('Y-m-01', strtotime($this->next->created));
            $till = date('Y-m-01',strtotime('+1 month', strtotime($from)));
            $attr->Set('href', JRoute::_("&id=".$this->next->id."&from=$from&till=$till"));
            $attr->Set('class', 'next-nav');
            $attr->Set('class', 'directionArrow');
        } else $attr->Set('class', 'next-nav-dummy');
        return sprintf('<a %s><img src="%s" alt="next" /></a>'
                , $attr
                , '/'.join('/', array('templates', 'hiled','images','body','article','next.gif'))
        );
    }
    public function Prev() {
        $attr = new VA_AttributeDictionary();
        $attr->Set('class', 'navigation');
        if(null !== $this->prev) {
            $from = date('Y-m-01', strtotime($this->prev->created));
            $till = date('Y-m-01',strtotime('+1 month', strtotime($from)));
            $attr->Set('href', JRoute::_("&id=".$this->prev->id."&from=$from&till=$till"));
            $attr->Set('class', 'prev-nav');
            $attr->Set('class', 'directionArrow');
        } else $attr->Set('class', 'prev-nav-dummy');
        return sprintf('<a %s><img src="%s" alt="prev" /></a>'
                , $attr
                , '/'.join('/', array('templates', 'hiled','images','body','article','prev.gif'))
        );
    }

}